const { Backend } = require('dx-router-admin');

module.exports = class IndexController extends Backend {
	constructor(ctx) {
		super(ctx);
	}
	async index({ group = 'base' }) {
		const [
			config,
			allUser,
			todayUser,
			toDayLogin,
			weekRegUsers,
			weekLoginUsers,
			threeDayUser,
			sevenDayUser,
			applicationUsers,
			allResource,
			todayResource,
		] = await Promise.all([
			this.$dx.service.config.where('group.name', group).select(),
			// 获取用户总数
			this.$dx.service.user.count(),
			// 获取今日新增用户
			this.$dx.service.user.toDayUser(),
			// 获取今日登录用户
			this.$dx.service.user.toDayLogin(),
			// 七天内注册用户
			this.$dx.service.user.getWeekRegUser(),
			// 七天内登录用户
			this.$dx.service.user.getWeekLoginUser(),
			// 三天内新增用户
			this.$dx.service.user.threeDayUser(),
			// 七天内新增用户
			this.$dx.service.user.sevenDayUser(),
			// 按应用统计，有共同的用户，不能累计
			this.$dx.service.user.getApplicationUser(),
			// 总附件
			this.$dx.service.resource.count(),
			// 今日新增附件
			this.$dx.service.resource.todayResource(),
		]);

		let data = {};
		for (let item of config) {
			data[item.name] = item.value;
		}
		return this.$dx.success(this.$dx.$t('获取成功'), {
			config: data,
			allUser,
			todayUser,
			toDayLogin,
			weekRegUsers,
			weekLoginUsers,
			threeDayUser,
			sevenDayUser,
			applicationUsers,
			allResource,
			todayResource,
		});
	}
	// 获取城市列表
	async getCity({ type, parent_code }) {
		let list = [];
		if (parent_code) {
			list = await this.$dx.service.city.getCityByParentCode(parent_code);
		} else {
			list = await this.$dx.service.city.getCityByType(type);
		}
		return this.$dx.success('获取成功', list);
	}
	// 获取所有城市
	async getAllCity() {
		let list = await this.$dx.service.city.getAllCity();
		return this.$dx.success('获取成功', list);
	}
	// 根据code获取城市列表
	async getCityByCode({ codes, code }) {
		let list = await this.$dx.service.city.getCityByCodes(codes || code);
		return this.$dx.success('获取成功', list);
	}
};
